package com.openbravo.pos.printer.config;

import java.awt.Component;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;

import cn.ekuma.data.dao.bean.IKeyed;

import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.pos.config.ParametersConfig;
import com.openbravo.pos.util.StringParser;

public class ParametersComm extends JPanel implements ParametersConfig{

	private JComboBox jcboConnPrinter;
	private JComboBox jcboSerialPrinter;

	/**
	 * Create the panel.
	 */
	public ParametersComm() {
		initComponent();
		
		jcboConnPrinter.addItem("serial");
        jcboConnPrinter.addItem("file");
		
        jcboSerialPrinter.addItem("COM1");
        jcboSerialPrinter.addItem("COM2");
        jcboSerialPrinter.addItem("COM3");
        jcboSerialPrinter.addItem("COM4");
        jcboSerialPrinter.addItem("LPT1");
        jcboSerialPrinter.addItem("/dev/ttyS0");
        jcboSerialPrinter.addItem("/dev/ttyS1");
        jcboSerialPrinter.addItem("/dev/ttyS2");
        jcboSerialPrinter.addItem("/dev/ttyS3");

	}

	private ComboBoxValEntry buildEntry(String prop) {
		String localkey="config.general."+prop;
        ComboBoxValEntry entry=new ComboBoxValEntry(prop,"");
        return entry;
	}

	private void initComponent() {
		JPanel panel = new JPanel();
		add(panel);
		
		JLabel label = new JLabel();
		label.setText("模式");
		
	   jcboConnPrinter = new JComboBox();
		
		JLabel label_1 = new JLabel();
		label_1.setText("端口");
		
		jcboSerialPrinter = new JComboBox();
		jcboSerialPrinter.setEditable(true);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(label, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(jcboConnPrinter, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(jcboSerialPrinter, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(92, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 24, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(jcboConnPrinter, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1)
						.addComponent(jcboSerialPrinter, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(195))
		);
		panel.setLayout(gl_panel);
		
	}

	@Override
	public Component getComponent() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void addDirtyManager(DirtyManager dirty) {
		jcboConnPrinter.addActionListener(dirty);
		jcboSerialPrinter.addActionListener(dirty);
	}

	@Override
	public void setParameters(StringParser p) {	    
	       jcboConnPrinter.setSelectedItem(unifySerialInterface(p.nextToken(',')));
	       jcboSerialPrinter.setSelectedItem(p.nextToken(','));
		
	}

	@Override
	public String getParameters() {
		return jcboConnPrinter.getSelectedItem() + "," + jcboSerialPrinter.getSelectedItem();
	}

	private String comboValue(Object value) {
        if(value == null)
            return "";
        if(value instanceof IKeyed)
            return ((IKeyed)value).getKey().toString();
        return value.toString();
    }
	
	private ComboBoxValEntry buildIKey(String prop){
        String localkey="config.general."+prop;
        ComboBoxValEntry entry=new ComboBoxValEntry(prop,"");
        return entry;
    }
	
	private String unifySerialInterface(String sparam) {
        // for backward compatibility
        return ("rxtx".equals(sparam))
                ? "serial"
                : sparam;
    }
}
